package cn.itcast.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class SpringSecurityService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        System.out.println("用户名为："+ username);
//        return null;
//        String password = "aaa";
//        List<GrantedAuthority> list = new ArrayList<>();
//        list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
//        return new User(username,"{noop}"+password,list);

        String password = "1234";//从数据库中根据用户名查询,密码 ;

        //从数据库中根据用户名查询用户角色权限信息 ;
        List<GrantedAuthority>  list = new ArrayList<GrantedAuthority>();
        if ("admin".equals(username)){
            list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        if ("xjj".equals(username)) {
            list.add(new SimpleGrantedAuthority("delete"));
        }
        if ("xgg".equals(username)) {
            list.add(new SimpleGrantedAuthority("ROLE_SELLER"));
        }
        return new User(username,"{noop}"+password,list);// 密码明文要在前面加{noop}
    }
}
